# -*- coding: UTF-8 -*-
# 读取excel数据
# 获取case对象list
# 对bug描述分词和统计词频

import xlrd
from model import *
import sys
import jieba.analyse
import jieba.posseg as pseg

reload(sys)
sys.setdefaultencoding('utf8')

# 所有case对象的list
caselist = []
# 关键词的list
taglist = []
testerset = set()
# 定义bug详情汇总
bugdetailsSummary = ''
# 定义提取关键词的数量
topK = 10

# 打开一个workbook
workbook = xlrd.open_workbook('jiajiayundong.xls')
# 抓取所有sheet页的名称
worksheets = workbook.sheet_names()
# print('抓取的Sheet是 %s' % worksheets)

# 抓取sheet对象
sheet1 = workbook.sheet_by_name('jiajiayundong')

# 获取行的总数
nrows = sheet1.nrows
# print '共有%s个样例' % nrows

# 获取样例对象
for i in range(nrows):
    caseid = int(sheet1.cell_value(i, 0))
    bugdetails = sheet1.cell_value(i, 1)

    newcase = case(caseid)
    newcase.bugdetails = bugdetails

    # 获取样例对象的截图list
    def getscreenshotslist(data):
        data1 = data.strip()
        data2 = data1.replace("\n\n", "\n")
        data3 = data2.split("\n")
        return data3


    newcase.screenshotslist = getscreenshotslist(sheet1.cell_value(i, 2))
    newcase.tester = int(sheet1.cell_value(i, 3)) # tester
    bugdetailsSummary = bugdetailsSummary + bugdetails  # 将bug详情汇总


    testerset.add(newcase.tester)

    caselist.append(newcase)  # 增加到样例队列


# 基于 TF-IDF 算法的关键词抽取
tags = jieba.analyse.extract_tags(bugdetailsSummary, topK=topK, withWeight=True)
count = 1
print '关键字和权重:'
for tag in tags:
    print ("num:%d\t\t tag: %s\t\t weight: %f" % (count, tag[0], tag[1]))
    taglist.append(tag[0])
    count += 1

# 根据词性分词，去重，根据词性过滤一部分词
# seg_list = pseg.cut(bugdetailsSummary)
# x = set(seg_list)
# cixing = set()
# for word, flag in x:
#     cixing.add(flag)
# y = list(cixing)
# y.sort()
# print '分词之后的词性有：'
# print y
# tags = []
# word_filter = ['a', 'ad', 'd', 'eng', 'f', 'i', 'l', 'n', 'nr', 'nrt', 'ns', 'nz', 'v', 'vn']
# word_filter1 = ['n', 'nr', 'nrt', 'ns', 'nz', 'v', 'vn']
# for word, flag in x:
#     if flag in word_filter1:
#         tags.append([word, flag])
#         taglist.append(word)
topK = len(taglist)
print topK

# 遍历bug详情，如果出现了tags中的关键词，则加入keywordslist
def searchkeywords(bugdetails):
    keywordslist = []
    for tag1 in tags:
        if bugdetails.find(tag1[0]) != -1:
            keywordslist.append(tag1[0])
    return keywordslist


for i in caselist:
    i.keywordslist = searchkeywords(i.bugdetails)
    # i.print_case()

count  = 0
print len(testerset)
for tester in testerset:
    picset = set()




    testernum = 0
    for i in caselist:
        if i.tester == tester:
            testernum += 1

    if testernum >= 2 :
        for i in caselist:
            if i.tester == tester:
                for j in i.screenshotslist:
                    picset.add(j)
        for i in caselist:

            i.screenshotslist.sort()
            pic = list(picset)
            pic.sort()
            if i.screenshotslist == pic:
                count += 1


print count


